/*
 * Copyright (c) 2011, Nikolaus Moll
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * * Neither the name of the gradecalculator.net nor the
 *   names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 */
package net.gradecalculator.gui.qt.editor.dialogs;


import net.gradecalculator.core.interfaces.IStudyRegulations;
import net.gradecalculator.gui.qt.common.dialogs.AbstractDialog;
import net.gradecalculator.gui.qt.common.layouts.VerySimpleLayout;
import net.gradecalculator.gui.qt.common.util.StringUtil;
import net.miblounge.miglayout.qt.MigLayout;

import com.trolltech.qt.gui.QComboBox;
import com.trolltech.qt.gui.QGroupBox;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QLineEdit;
import com.trolltech.qt.gui.QWidget;

public class StudyRegulationsDescriptionDialog extends AbstractDialog {
	
	private QLineEdit leName;
	private QLineEdit leSemester;
	private QComboBox cbUniversity;
	private QComboBox cbDegree;
	
	private final IStudyRegulations reg;
	
	public StudyRegulationsDescriptionDialog(final QWidget parent, final IStudyRegulations reg)
	{
		super(parent);
		setWindowTitle(tr("Regulations"));
		
		this.reg = reg;

		setClientArea(createClientArea());

		leName.setFocus();
		leName.setSelection(0, leName.text().length());
		
		revalidate();
	}
	
	private QWidget createClientArea()
	{
		final QGroupBox group = new QGroupBox(tr("Regulations"), this);
		final VerySimpleLayout layoutGroup = new VerySimpleLayout(group);
		group.setLayout(layoutGroup);

		final QWidget container = new QWidget(group);
		layoutGroup.setWidget(container);
		final MigLayout containerLayout = new MigLayout("", "[][grow,fill]15[grow]"); 
		container.setLayout(containerLayout);
		
		containerLayout.addWidget(new QLabel(tr("Name:")));
		leName = new QLineEdit(reg.getModuleList().getName(), container);
		leName.textEdited.connect(this, "revalidate()");
		containerLayout.addWidget(leName, "span, growx, w 100:200:");

		containerLayout.addWidget(new QLabel(tr("University:")));
		cbUniversity = new QComboBox(container);
		cbUniversity.setEditable(true);
		cbUniversity.setEditText(reg.getUniversity().getName());
		cbUniversity.editTextChanged.connect(this, "revalidate()");
		containerLayout.addWidget(cbUniversity, "span, growx, w 100:200:");

		containerLayout.addWidget(new QLabel(tr("Degree:")));
		cbDegree = new QComboBox(container);
		cbDegree.setEditable(true);
		cbDegree.setEditText(reg.getDegree());
		cbDegree.editTextChanged.connect(this, "revalidate()");
		containerLayout.addWidget(cbDegree, "growx, w 100:200:200");
		
		containerLayout.addWidget(new QLabel(tr("Semester:")), "split");
		leSemester = new QLineEdit(reg.getVersionOfRegulations(), container);
		leSemester.textEdited.connect(this, "revalidate()");
		containerLayout.addWidget(leSemester, "span, growx, w 100!");

		return group;
	}
	
	protected boolean validate()
	{
		if (StringUtil.cleanString(leName.text()).equals("")) {
			return false;
		}

		if (StringUtil.cleanString(leSemester.text()).equals("")) {
			return false;
		}

		if (StringUtil.cleanString(cbUniversity.currentText()).equals("")) {
			return false;
		}

		if (StringUtil.cleanString(cbDegree.currentText()).equals("")) {
			return false;
		}
		
		return super.validate();
	}

	
	public void apply()
	{
		super.apply();
		reg.getModuleList().setName(StringUtil.cleanString(leName.text()));
		// TODO load university
		//reg.setUniversity(StringUtil.cleanString(cbUniversity.currentText()));
		reg.setDegree(StringUtil.cleanString(cbDegree.currentText()));
		reg.setVersionOfRegulations(StringUtil.cleanString(leSemester.text()));
	}

}
